package com.cskaoyan.thread.introduction.edition1;

import java.util.Scanner;

/**
 * @author zhangshuai@Cskaoyan.onaliyun.com.
 * @version 1.0
 */
public class Demo1 {

  static boolean  flag = true;


  public static void main(String[] args) {

    // 先不停的输出问候语句
    sayHelloRecycling();

    //接收键盘输入，根据键盘输入，决定是否输出问候语句
    waitToStop();

  }


  // 接收键盘输入
  public static void waitToStop() {
    Scanner scanner = new Scanner(System.in);
    while (true) {
      String s = scanner.nextLine();
      if ("gun".equals(s)) {
        flag = false;
        break;
      }
    }
  }

  // 不停的向屏幕输出问候语句
  public static void sayHelloRecycling() {
    while (flag) {
      System.out.println("哈哈，你好");
      try {
        // 暂停3秒
        Thread.sleep(3000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }

}
